package com.it.homework;

import java.time.Period;

/**
 * @author げっしょく
 * @date 2021/8/7
 */
public class Test2 {
    public static void main(String[] args) {
        // 死锁实现
        Object o1 = new Object();
        Object o2 = new Object();
        Test2T1 t1 = new Test2T1(o1, o2);
        Test2T2 t2 = new Test2T2(o1, o2);
        t1.start();
        t2.start();
    }
}

class Test2T1 extends Thread {
    private Object o1;
    private Object o2;

    public Test2T1(Object o1, Object o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o2){
            try {
                sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o1){

            }
        }
    }
}

class Test2T2 extends Thread {
    private Object o1;
    private Object o2;

    public Test2T2(Object o1, Object o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o1){
            try {
                sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o2){

            }
        }

    }
}
